﻿//********************************************************************************************
//** 					        NoSleepHD v1.1                     	                        **
//**A Simple program to stop your external hard-drive from going ot Auto-Sleep mode.        **
//********************************************************************************************
//**    NoSleepHD   2009 Ashutosh Agarwal. All Rights Reserved      			            **
//**    Version:    1.1 	                                         		            	**
//**    Notes:      Added Startup auto-run feature.                                         **
//**                Retain User Settings                                                    **
//**                Improved Error Checking	            	                                **
//**                Sorry for the Uncommented code.       	                                **
//********************************************************************************************

//How to use the program:

//1. Select any drive on your external hard disk OR a folder on your external hard disk drive.
//2. Set the timer. (10 Minutes Recommended)
//3. Press 'Start NoSleep Mode'.

//And that is it! No more irritating waiting/connection problem with your external hard disk.

//You could stop the program if you want to. (Which would allow the drive to go to auto-sleep 
//after a few minutes.

//You could also choose the applicaiton to sit in the System Tray or simply minimized.


//Any Comments, Questions, Suggestions, Requests should go to: agarwal@hotmail.co.uk

//Product Home Page: http://nosleephd.codeplex.com/

//Thank you for using the program.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Timers;
using System.Runtime.InteropServices;
using System.Configuration;

namespace NoSleepHD
{
    public partial class Form1 : Form
    {
        
        public Form1()
        {
            InitializeComponent();
        }



        private void Form1_Load(object sender, EventArgs e)
        {
            label2.Text = Properties.Settings.Default["WritePath"].ToString();
            numericUpDown1.Value = Convert.ToDecimal(Properties.Settings.Default["Minutes"]);
            string tmr = Convert.ToString(Properties.Settings.Default["Timer"]);
            string checkbox = Convert.ToString(Properties.Settings.Default["CheckBox"]);

            if (checkbox != "False")
            {
                checkBox1.Checked = true;
            }

            if (tmr == "False")
            {
                timer1.Enabled = false;
            }

            else
            {
                timer1.Start();
                label8.ForeColor = Color.Green;
                label8.Text = "ON";
            }

            if (checkbox == "True")
            {
                Hide();
            }
            else
            {
                WindowState = FormWindowState.Normal;
            }

        }
        
        int i = 0;
        Microsoft.Win32.RegistryKey NoSleepHD = Microsoft.Win32.Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);


        private void button1_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog tree = new FolderBrowserDialog();
            tree.RootFolder = Environment.SpecialFolder.MyComputer;
            tree.ShowNewFolderButton = false;
            tree.Description = "Please Select any Drive OR Folder on Your External hard Drive";
            tree.ShowDialog();
            if (tree.SelectedPath.Length == 0)
            {
            }
            else
            {
                label2.Text = tree.SelectedPath.ToString();
                Properties.Settings.Default.WritePath = label2.Text.ToString();
                Properties.Settings.Default.Save();
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            timer1.Enabled = true;
            timer1.Start();
            label8.ForeColor = Color.Green;
            label8.Text = "ON";
            Properties.Settings.Default.Timer = timer1.Enabled;
            Properties.Settings.Default.Save();

            

        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (timer1.Enabled == false)
            {
                //MessageBox.Show("Please Start the timer first to Stop it :)");
            }
            else
            {
                timer1.Stop();
                timer1.Enabled = false;
                label8.ForeColor = Color.Red;
                label8.Text = "OFF";
                Properties.Settings.Default.Timer = timer1.Enabled;
                Properties.Settings.Default.Save();
                checkBox1.Checked = false;
            }

        }


        private void timer1_Tick(object sender, EventArgs e)
        {
            i = i + 1;
            if (i >= (numericUpDown1.Value * 6))
            {
                // Touch File and reset i's value.
                FileStream fs = new FileStream(label2.Text + "test.txt", FileMode.OpenOrCreate, FileAccess.Write);
                StreamWriter sw = new StreamWriter(fs);
                sw.Write("NoSleepHD v1.1 Tick! By Ashutosh Agarwal - agarwal@hotmail.co.uk");
                sw.Close();
                fs.Close();
                i = 0;
            }


        }

        private void button4_Click(object sender, EventArgs e)
        {
            Hide();
        }

        private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            Show();
            WindowState = FormWindowState.Normal;
        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            MessageBox.Show("Please forward any Questions, Comments, Suggestions, Requests to: " +
                            "\n\nagarwal@hotmail.co.uk\n\nThank You for Using the Program",
                            "NoSleepHD v1.1", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        private void button6_Click(object sender, EventArgs e)
        {
            MessageBox.Show("A Simple program which writes an empty text file every few minutes to " +
                "your external hard disk drive to keep it from going into auto-sleep mode." +
                "\nUsually auto sleep timer in external hard disks is 10 Minutes.", "NoSleepHD v1.0 | By - Ashutosh Agarwal",
                MessageBoxButtons.OK,
                MessageBoxIcon.Information);
        }

        private void checkBox1_CheckStateChanged(object sender, EventArgs e)
        {
            if (checkBox1.Checked == true)
            {
                NoSleepHD.SetValue("NoSleepHD", Application.ExecutablePath.ToString() + " /m");
                label6.ForeColor = Color.Green;
                label6.Text = "Enabled";
                Properties.Settings.Default.CheckBox = checkBox1.Checked;
                Properties.Settings.Default.Save();


                
            }
            else
            {
                if (checkBox1.Checked == false)
                {
                    NoSleepHD.DeleteValue("NoSleepHD", false);
                    label6.Text = "Disabled";
                    label6.ForeColor = Color.Red;
                }
            }



                             
        }

        private void numericUpDown1_ValueChanged(object sender, EventArgs e)
        {
            Properties.Settings.Default.Minutes = Convert.ToDecimal(numericUpDown1.Value);
            Properties.Settings.Default.Save();
        }

    }
}
